<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
set_time_limit(0);
class Timed extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	public function index() {//定时功能周六11点50分,复制上周菜谱到本周
		$this->load->model ( 'school_model' );
		$this->load->model ( 'cookbook_model' );
		$this->load->model ( 'user_model' );
		$schooles=$this->school_model->find_all_school();
		foreach($schooles as $school){
			/*
			$orderdate= $this->common->getOrderDate($school['id']);
			var_dump($orderdate);
			die();
			foreach($orderdate as $item){
				$date=date("Y-m-d",strtotime("$item +1 week"));
				for($i=1;$i<=3;$i++){
					$temp=$this->cookbook_model->find_product_by_date_type($school['id'],$date,$i);
					if(!empty($temp)){//本周菜谱非空跳过
						continue;
					}
					$cookbookes=$this->cookbook_model->find_product_by_date_type($school['id'],$item,$i);
					if(empty($cookbookes)){//上周菜谱为空跳过
						continue;
					}
					foreach($cookbookes as $cookbook){
						$newcookbook=array (
							'schoolid' => $cookbook['schoolid'],
							'date' => $date,
							'productid' => $cookbook['productid'],
							'type' => $cookbook['type'],
							'add_time' => date ( "Y-m-d H:i:s" ),
							'adminid' => $cookbook['adminid'],
							'daylim' => $cookbook['daylim'],
							'stock' => $cookbook['daylim']
						);
						$this->cookbook_model->create ( $newcookbook );
					}
				}
			}*/
		}
		$users = $this->user_model->find_all_users ();
		foreach($users as $user){
			$this->createOrder($user['id'],$user['school'],$user['order_state']);
		}
		echo "运行成功！";
	}
	public function index2() {//定时功能周日0点0分
		$this->load->model ( 'order_model' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'shoppingcart_model' );
		$this->shoppingcart_model->clear();//清空购物车
		//$this->order_model->update_orders(2);//完成订单
		$this->order_model->update_orders(1);//确认订单
		$this->user_model->update_users ();//恢复用户下单状态
		echo "运行成功！";
	}
	/*
	 * 生成默认订单
	 */
	private function createOrder($userid,$schoolid,$orderstate) {
		$sdate = getSdate();//下周二
		$edate = getEdate();//下下周一
		$this->load->model ( 'product_model' );
		$this->load->model ( 'order_model' );
		$this->load->model ( 'orderdetail_model' );
		$this->load->model ( 'cookbook_model' );
		$product=$this->product_model->find_by_category ( 4 );//默认餐
		if(!empty($product)){
			if($orderstate==0){//未下单
				$orderno = build_order_no ($userid);
				$order = array (
					'orderno' => $orderno,
					'userid' => $userid,
					'order_time' => date ( "Y-m-d H:i:s" ),
					'total' => 0,
					'state' => 0,
					'defaults' => 1
				);
				$orderid=$this->order_model->create ( $order );
				if (!empty($orderid)) {//插入的新数据ID不为空
					for($start = strtotime($sdate); $start <= strtotime($edate);$start += 3600*24){
						$date=date ( "Y-m-d", $start);
						for($i=1;$i<=3;$i++){
							$temp=$this->cookbook_model->count_by_school_date_type($schoolid,$date,$i);//本周该餐是否有菜谱
							if(empty($temp)){
								continue;
							}
							$orderdetail=array (
								'orderid' => $orderid,
								'productid' => $product['id'],
								'price' => $product['price'],
								'amount' => 1,
								'date' =>$date,
								'type' =>$i,
								'defaults' => 1
							);
							$this->orderdetail_model->create ( $orderdetail );
						}
					}
				}
				$newuser = array (
					'order_state' => 1
				);
				$this->user_model->update ( $userid, $newuser );
			}else{
				$order=$this->order_model->find_order_by_user($userid);
				for($start = strtotime($sdate); $start <= strtotime($edate);$start += 3600*24){
					$date=date ( "Y-m-d", $start);
					for($i=1;$i<=3;$i++){
						$temp=$this->cookbook_model->count_by_school_date_type($schoolid,$date,$i);//本周该餐是否有菜谱
						if(empty($temp)){
							continue;
						}
						$temp2=$this->orderdetail_model->count_by_order_date_type($order['id'],$date,$i);//订单里该餐是否订购
						if(!empty($temp2)){
							continue;
						}
						$orderdetail=array (
							'orderid' => $order['id'],
							'productid' => $product['id'],
							'price' => $product['price'],
							'amount' => 1,
							'date' =>$date,
							'type' =>$i,
							'defaults' => 1
						);
						$this->orderdetail_model->create ( $orderdetail );
					}
				}
			}
		}
	}
}